<?php
namespace api\user\service\UserServer;

use api\user\controller\PublicController;
use api\user\model\UserModel;
use api\user\model\UserOtherInfoModel;
use api\user\service;
use cmf\controller\RestBaseController;
use think\Db;

class AgentStrategy extends userStrategy
{
    public function createUser($data = [])
    {
        $publics = new PublicController();
        $RestBase = new RestBaseController();
        $DeviceCountNum = $RestBase->getDeviceCountNum();
        $surplus = $DeviceCountNum['count'] - $DeviceCountNum['surplus'];
        if ($data['device_num'] > $surplus) {
            return 2;
        }
        $name = cmf_check_mobile($data['username']) ? 'mobile' : 'user_email';
        $User = new UserModel();
        $UserOtherInfo = new UserOtherInfoModel();
        $count = $User->where($name, $data['username'])->count();
        if ($count) {
            return 3;
        }
        //
        Db::startTrans();
        try {
            //添加用户
            $user = [
                'user_pass' => cmf_password($data['password']),
                'user_code' => $publics->userCode(),
                $name => $data['username'],
                'device_num' => $data['device_num'],
                'user_type' => 3,
                'user_status' => 1,
                'create_time' => time(),
                'parent_id' => $RestBase->getUserId(),
            ];
            $user_id = $User->insertGetId($user);

            //添加用户辅助信息
            $UserOtherInfoData = [
                'user_info_name' => $data['user_info_name'],
                'user_info_phone' => $data['user_info_phone'],
                'user_info_address' => $data['user_info_address'],
                'user_id' => $user_id,
                'create_time' => time(),
            ];
            $UserOtherInfo->insert($UserOtherInfoData);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return 0;
        }
        return 1;

    }

    /**
     * [editUser 编辑用户]
     * @Author   HUI
     * @DateTime 2019-01-08
     * @version  [version]
     * @param    string     $value [description]
     * @return   [type]            [description]
     */
    public function editUser($data = [])
    {
        $publics = new PublicController();
        $RestBase = new RestBaseController();
        $DeviceCountNum = $RestBase->getDeviceCountNum();
        $surplus = $DeviceCountNum['count'] - $DeviceCountNum['surplus'];
        if ($data['device_num'] > $surplus) {
            return 2;
        }
        $User = new UserModel();
        $UserOtherInfo = new UserOtherInfoModel();
        //
        Db::startTrans();
        try {
            //添加用户
            // $name = cmf_check_mobile($data['username']) ? 'mobile' : 'user_email';
            // $count = $User->where($name, $data['username'])->count();
            //判断用户是否需要更改密码
            if (isset($data['password']) && !empty($data['password'])) {
                $user['user_pass'] = cmf_password($data['password']);
            }
            $user = [
                'user_code' => $publics->userCode(),
                'device_num' => $data['device_num'],
                // $name => $data['username'],
            ];

            $user_id = $User->where(['id' => $data['user_id'], 'parent_id' => $RestBase->getUserId()])->update($user);
            //添加用户辅助信息
            $UserOtherInfoData = [
                'user_info_name' => $data['user_info_name'],
                'user_info_phone' => $data['user_info_phone'],
                'user_info_address' => $data['user_info_address'],
            ];
            $UserOtherInfo->where('user_id', $data['user_id'])->update($UserOtherInfoData);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return 0;
        }
        return 1;
    }

    public function killUser($data = [])
    {
        $RestBase = new RestBaseController();
        $user['user_status'] = 0;
        if (Db::name("user")->where(['id' => $data['user_id'], 'parent_id' => $RestBase->getUserId()])->update($user)) {
            return 1;
        } else {
            return 0;
        }
    }

    public function releaseUser($data = [])
    {
        $RestBase = new RestBaseController();
        $user['user_status'] = 1;
        if (Db::name("user")->where(['id' => $data['user_id'], 'parent_id' => $RestBase->getUserId()])->update($user)) {
            return 1;
        } else {
            return 0;
        }
    }


    public function getUserInfo($data = [])
    {
        $User = new UserModel();
        return $User->alias("a")
            ->join('user_other_info t', 't.user_id = a.id')
            ->where(['a.id' => $data['user_id']])
            ->find();
    }


    public function getAllUser($data = [])
    {
        $User = new UserModel();
        $RestBase = new RestBaseController();
        return $User->alias("a")
            ->field('a.user_status,a.user_type,a.create_time,a.device_num,u.*,count(t.id) as open_sum,a.device_num-count(t.id) as surplus_num')
            ->join('user_other_info u', 'u.user_id = a.id')
            ->join('toilet t', 't.user_id = a.id AND t.status = 1','LEFT')
            ->join('band_toilet b', 'b.t_id = t.id','LEFT')
            ->where(['a.parent_id' => $RestBase->getUserId()])
            ->group('a.id')
            ->select();
        
    }



}
